﻿using Engine;
using Game;
using Mekiasm.Common;
using Mekiasm.Dust;
using Mekiasm.ElectricElement;
using Mekiasm.Machine;
using System;
using System.Collections.Generic;

namespace Mekiasm.Machines
{
    public class MetallurgicInfuser : MachineRotateAbleItem, Interface.IDynamicCraft
    {
        public static int Index;

        public MetallurgicInfuser() : base("冶金灌注机")
        {
            textureSlots = new int[] { 49, 48, 50, 48, 51, 51 };
        }

        public int[] CraftSlotIndies => new int[] { 1 };

        public int[] FuelCraftSlotIndies => new int[] { 0 };

        public int[] ResultSlotIndies => new int[] { 2 };

        public int[] RemainSlotIndies => new int[0];

        public int SlotsCount => 3;

        public int BlockValue => blockValue;

        public Point2 CraftSize => new Point2(2, 1);

        public void GenerateRecipes(List<MekRecipe> mekRecipe)
        {
            mekRecipe.Add(new MekRecipe(1, 1, 1).SetName("钢粉")
                .SetIngredian(0, ILibrary.GetCraftId(CoalDust.Index), 10, true, ComponentEnergyMachine.FuelType.Dust)
                .SetIngredian(0, ILibrary.GetCraftId(IronIngotBlock.Index))
                .SetResult(0, SteelDust.Index).SetRemainNeedCheck());
            mekRecipe.Add(new MekRecipe(1, 1, 1).SetName("富集合金")
                .SetIngredian(0, ILibrary.GetCraftId(RedStoneDust.Index), 10, true, ComponentEnergyMachine.FuelType.Dust)
                .SetIngredian(0, ILibrary.GetCraftId(IronIngotBlock.Index))
                .SetResult(0, EnrichedAlloy.Index).SetRemainNeedCheck());
            mekRecipe.Add(new MekRecipe(1, 1, 1).SetName("强化合金")
                .SetIngredian(0, ILibrary.GetCraftId(DiamondDust.Index), 10, true, ComponentEnergyMachine.FuelType.Dust)
                .SetIngredian(0, ILibrary.GetCraftId(IronIngotBlock.Index))
                .SetResult(0, ReinforcedAlloy.Index).SetRemainNeedCheck());
            mekRecipe.Add(new MekRecipe(1, 1, 1).SetName("强化陨石粉")
                .SetIngredian(0, ILibrary.GetCraftId(DiamondDust.Index), 10, true, ComponentEnergyMachine.FuelType.Dust)
                .SetIngredian(0, ILibrary.GetCraftId(SkyStoneDust.Index))
                .SetResult(0, RefinedObsidianDust.Index).SetRemainNeedCheck());
            mekRecipe.Add(new MekRecipe(1, 1, 1).SetName("强化陨石锭")
                .SetIngredian(0, ILibrary.GetCraftId(OsmiumDust.Index), 10, true, ComponentEnergyMachine.FuelType.Dust)
                .SetIngredian(0, ILibrary.GetCraftId(RefinedObsidianDust.Index))
                .SetResult(0, RefinedObsidianIgnot.Index).SetRemainNeedCheck());
            mekRecipe.Add(new MekRecipe(1, 1, 1).SetName("原子合金")
                .SetIngredian(0, ILibrary.GetCraftId(SkyStoneDust.Index), 10, true, ComponentEnergyMachine.FuelType.Dust)
                .SetIngredian(0, ILibrary.GetCraftId(ReinforcedAlloy.Index))
                .SetResult(0, AtomicAlloy.Index).SetRemainNeedCheck());
        }

        public void GenerateRecipeWidget(CanvasWidget canvas, ComponentEnergyMachine energyMachine, ComponentMiner miner, ref Action Tick)
        {
            var leftCanvas = new CanvasWidget() { Size = new Vector2(96, 128) ,HorizontalAlignment=WidgetAlignment.Center,VerticalAlignment=WidgetAlignment.Center};
            var h1 = new StackPanelWidget() { Direction = LayoutDirection.Horizontal, VerticalAlignment = WidgetAlignment.Center, HorizontalAlignment = WidgetAlignment.Center };
            var v1 = new StackPanelWidget() { Direction = LayoutDirection.Horizontal, VerticalAlignment = WidgetAlignment.Center, HorizontalAlignment = WidgetAlignment.Center };
            var fp1 = new FluidBaseProgress(new Vector2(16, 128), energyMachine.Slots[0]) { HorizontalAlignment = WidgetAlignment.Center, VerticalAlignment = WidgetAlignment.Far };
            var s1 = new MekInventorySlotWidget() { Size=new Vector2(48),VerticalAlignment=WidgetAlignment.Far, Margin = new Vector2(15, 0)};
            var craftSlotWidget = new InventorySlotWidget() { Size = new Vector2(48) ,VerticalAlignment=WidgetAlignment.Center};
            var resultSlotWidget = new InventorySlotWidget() { Size = new Vector2(48) ,VerticalAlignment=WidgetAlignment.Center};
            var p1 = new MekProgressWidget(MekProgressWidget.ProgessType.large_right) { Size = new Vector2(64, 16), Margin = new Vector2(15, 0), VerticalAlignment = WidgetAlignment.Center };
            s1.AssignInventorySlot(energyMachine,0);s1.ConfigSlotMode(miner);
            craftSlotWidget.AssignInventorySlot(energyMachine, 1); resultSlotWidget.AssignInventorySlot(energyMachine, 2);
            v1.AddChildren(fp1);v1.AddChildren(s1);
            leftCanvas.AddChildren(v1);
            h1.AddChildren(leftCanvas); h1.AddChildren(craftSlotWidget);h1.AddChildren(p1);h1.AddChildren(resultSlotWidget);
            canvas.AddChildren(h1);
            Tick += () => {
                fp1.SetProgress(energyMachine.Slots[0].FuelAmount / (float)energyMachine.Slots[0].FuelCapacity);
                p1.SetProgress(energyMachine.CraftProgress);
            };
        }

        public override MekBlock.DeviceType GetDeviceType()
        {
            return MekBlock.DeviceType.Electric;
        }

        public override bool IsInteractive(SubsystemTerrain subsystemTerrain, int value)
        {
            return true;
        }

        public override void OnBlockAdded(SubsystemBlockBehavior blockBehavior, int value, int oldValue, int x, int y, int z)
        {
            ILibrary.AddBaseEnergyEntity(DisplayName, x, y, z, new List<ComponentEnergyMachine.Slot>() { //格子初始化
                new ComponentEnergyMachine.Slot(){ FuelCapacity = 1000 }
            }, ILibrary.GetRotetedConfig(value, new MachineFaceConfig[] {
            new MachineFaceConfig(MekBlock.MachineMode.IN,MekBlock.DeviceType.Electric),
            new MachineFaceConfig(MekBlock.MachineMode.IN,MekBlock.DeviceType.Electric),
            new MachineFaceConfig(MekBlock.MachineMode.IN,MekBlock.DeviceType.Electric),
            new MachineFaceConfig(MekBlock.MachineMode.IN,MekBlock.DeviceType.Electric),
            new MachineFaceConfig(MekBlock.MachineMode.IN,MekBlock.DeviceType.Item),
            new MachineFaceConfig(MekBlock.MachineMode.IN,MekBlock.DeviceType.Item)
            }), new MachineAmountLimitConfig() { MaxPower = 10240, OutputPowerLimit = 32, InputPowerLimit = 32 });
            ElectricSystem.Add(new MetallurgicInfuserElement(x, y, z));
        }
        public override void OnBlockGenerated(int value, int x, int y, int z, bool isLoaded)
        {
            ElectricSystem.Add(new MetallurgicInfuserElement(x, y, z));
        }
        public override void OnBlockRemoved(SubsystemBlockBehavior blockBehavior, int value, int newValue, int x, int y, int z)
        {
            ILibrary.RemoveBlockEntity(x, y, z);
            ElectricSystem.Remove(new Point3(x, y, z));
        }
        public override bool OnInteract(SubsystemBlockBehavior blockBehavior, TerrainRaycastResult raycastResult, ComponentMiner componentMiner)
        {
            ComponentEnergyMachine machine = blockBehavior.Project.FindSubsystem<SubsystemBlockEntities>().GetBlockEntity(raycastResult.CellFace.X, raycastResult.CellFace.Y, raycastResult.CellFace.Z).Entity.FindComponent<ComponentEnergyMachine>(true);
            componentMiner.ComponentPlayer.ComponentGui.ModalPanelWidget = new MetallurgicInfuserWidget(machine, componentMiner);
            return true;
        }

    }
}
